import { httpUrl, ERR_OK } from 'api/config'
import { ajax } from 'api/axios'

export const login = (data) => {
  const url = `${httpUrl}/bindWx`

  return ajax(url, data)
}

export const tenantLogin = (data) => {
  const url = `${httpUrl}/rentor/login`

  return ajax(url, data)
}

export const wxLogin = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        if (res.code) {
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',
            data: {
              appid: 'wx3d7bb658653ecc54',
              secret: 'cbcfcad69aba8bf4771d70d173032762',
              js_code: res.code,
              grant_type: 'authorization_code'
            },
            success: (res2) => {
              resolve({
                code: ERR_OK,
                result: res2
              })
            },
            fail: (res2) => {
              resolve({
                code: '9999',
                result: res2
              })
            }
          })
        } else {
          resolve({
            code: '9999',
            ...res
          })
        }
      }
    })
  })
}
